package com.example.sy.service;

import com.example.sy.mapper.userMapper;
import com.example.sy.pojo.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class service {
 @Autowired
    private userMapper userMapper;
    public List<user> findAllStudent() {
        return userMapper.findAll();         //业务逻辑
    }

    public Map<String,Object> findUser(String token){
        List<user> list=new ArrayList<>();
        Map<String,Object> map=new HashMap<>();
        if(new user().analysis(token)==1){
            map.put("code",200);
            map.put("msg","success");
            map.put("data",userMapper.findUser(new user().getUsername(token)).get(0));
            return map;
        }
        else{
            return map;
        }

    }

    //新增user
    public int addUser(user user){
        List<user> list=new ArrayList<>();
        list =userMapper.findUser(user.username);
        //等于0 允许注册
        if(list.size()==0){
            return  userMapper.addUser(user);
        }
        //重新取名
        else{
            return  2;
        }
    }

    //删除
    public int deleteUser(user user){
        int code=userMapper.deleteUser(user);
        return  code;
    }

    //登录
    public List<user> userLogin(user user){
        return userMapper.userLogin(user);
    }
}
